/**
 * api2entity
 * <p>
 * Copyright 2014 Acooly.cn, Inc. All rights reserved.
 *
 * @author hesir
 * @date 2020-12-21 17:04
 */
package com.hesir.api2entity.util;

import com.hesir.api2entity.pojo.ApiEntity;
import com.hesir.api2entity.services.Excelhandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author hesir
 * @date 2020-12-21 17:04
 */
@Slf4j
public class FileUtils {
    /**
     * @Author hetianxing
     * @Description  生成Java文件
     * @Date  2019/12/23 14:40
     * @Param [fileName] 文件名
     * @Param [apiEntities] 字段列表
     * @Param [useCamel] 是否使用驼峰命名
     * @return void
     **/
    public static void createFile(String fileName,String entityDesc, List<ApiEntity> apiEntities,String folderPath,String type,String packagePath,Boolean useCamel){
        String filePath =  System.getProperty("user.dir")+"/src/main/java/com/hesir/api2entity/entity/dto/"+folderPath+"/"+type;;
        File folder = new File(filePath);
        File file = new File(filePath+"/"+fileName+".java");
        if (!folder.mkdirs()){
            folder.mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            StringBuilder str = new StringBuilder();
            str.append("/**\n" +
                    " * pocket-pf\n" +
                    " * \n" +
                    " * @author hesir\n" +
                    " * @date 2020-12-18 11:32\n" +
                    " */\n" +
                    "package "+packagePath+";\n" +
                    "\n" +
                    "import lombok.Data;\n" +
                    "\n");
            str.append("@Data\n@ApiModel(value = \""+entityDesc+"\")\n");
            str.append("public class "+fileName+" {\n");
            for (ApiEntity entity:apiEntities){
                String required = "";
                if ("true".equals(entity.getRequired().trim())){
                    required = "true";
                }else{
                    required = "false";
                }

                str.append("\t@ApiModelProperty(value = \""+entity.getComment()+"\", required = "+required+")\n");

                String title = entity.getName();
                if (useCamel){
                    title = Excelhandler.CamelConvert.underline2Camel(title);
                }
                str.append("\tprivate "+entity.getType()+" "+title+"; \n");
            }
            str.append("}");

            byte[] bytes = str.toString().getBytes();
            fos.write(bytes);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * @Author hetianxing
     * @Description  将下划线字段转换为驼峰格式
     * @Date  2019/12/23 14:40
     * @Param
     * @return
     **/
    public static class CamelConvert{
        public static final char UNDERLINE_CHAR = '_';

        /**
         * 下划线转驼峰
         *
         * @param underlineStr
         * @return
         */
        public static String underline2Camel(String underlineStr) {

            if (StringUtils.isEmpty(underlineStr)) {

                return "";
            }

            int len = underlineStr.length();
            StringBuilder strb = new StringBuilder(len);
            for (int i = 0; i < len; i++) {

                char c = underlineStr.charAt(i);
                if (c == UNDERLINE_CHAR && (++i) < len) {

                    c = underlineStr.charAt(i);
                    strb.append(Character.toUpperCase(c));
                } else {

                    strb.append(c);
                }
            }
            return strb.toString();
        }
    }
}
